package api

import (
	"context"
	"fmt"
	"net/http"

	commctrl "gitee.com/klein-x/biz/controller/comm"
	wxpayctrl "gitee.com/klein-x/biz/controller/wxpay"
	"gitee.com/klein-x/biz/xutils"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/adaptor"
	"github.com/cloudwego/hertz/pkg/common/utils"
)

type MakeOrderRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	GoodsId string  `form:"goodsId" json:"goodsId"`
}
type MakeOrderResponse struct {
	OrderId string `json:"order_id"`
}

// 生成订单
func MakeOrder(ctx context.Context, c *app.RequestContext) {
	var req MakeOrderRequest
	var res MakeOrderResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if orderId, err := commctrl.MakeOrder(ctx, uid, req.GoodsId); err != nil {
		ReturnError(c, err)
	} else {
		res.OrderId = *orderId
		ReturnSuccess(c, res)
	}
}

type OrderSummarysRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
}
type OrderSummarysResponse struct {
	Order *[]commctrl.OrderSummary `json:"order"`
}

// 查询订单列表
func OrderSummarys(ctx context.Context, c *app.RequestContext) {
	var req OrderSummarysRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if goodsDetailInfo, err := commctrl.GetUserOrderSummarys(ctx, uid); err != nil {
		ReturnError(c, err)
	} else {
		var resp OrderSummarysResponse
		resp.Order = goodsDetailInfo
		ReturnSuccess(c, resp)
	}
}

type OrderDetailRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	OrderId string  `form:"order_id" json:"order_id"`
}
type OrderDetailResponse struct {
	OrderDetail *commctrl.OrderDetail `json:"order_detail"`
}

// 查询订单详情
func OrderDetail(ctx context.Context, c *app.RequestContext) {
	var req OrderDetailRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}
	uid := c.GetString("id")
	if goodsDetailInfo, err := commctrl.GetUserOrderDetail(ctx, uid, req.OrderId); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp OrderDetailResponse
		resp.OrderDetail = goodsDetailInfo
		ReturnSuccess(c, resp)
	}
}

type WxPrePayRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	OrderId string  `form:"order_id" json:"order_id"`
}
type WxPrePayResponse struct {
	PrePayId string `json:"pre_pay_id"`
}

// 生成微信预支付订单
func WxPrePay(ctx context.Context, c *app.RequestContext) {
	var req WxPrePayRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}
	uid := c.GetString("id")
	if PrePayId, err := wxpayctrl.WxPrePay(ctx, uid, req.OrderId); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp WxPrePayResponse
		resp.PrePayId = PrePayId
		ReturnSuccess(c, resp)
	}
}

type WxPayResultRequest struct {
	Version  Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PrePayId string  `form:"pre_pay_id" json:"pre_pay_id"`
}
type WxPayResultResponse struct {
	PrePayId string `json:"pre_pay_id"`
}

// 查询微信支付结果
func WxPayResult(ctx context.Context, c *app.RequestContext) {
	var req WxPayResultRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}
	uid := c.GetString("id")
	if PrePayId, err := wxpayctrl.WxPayResult(ctx, uid, req.PrePayId); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp WxPayResultResponse
		resp.PrePayId = PrePayId
		ReturnSuccess(c, resp)
	}
}

// 微信支付通知回调
func WxPayNotify(ctx context.Context, c *app.RequestContext) {
	// 转换hertz的Request到http的Request
	req, err := adaptor.GetCompatRequest(&c.Request)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    "NotifyError",
			"message": fmt.Errorf("hertz.request conver to http.request error, %s", err.Error()),
		})
		return
	}

	code, err := wxpayctrl.WxPayNotify(ctx, req)
	if err != nil {
		c.JSON(code, utils.H{
			"code":    "NotifyError",
			"message": err.Error(),
		})
	} else {
		c.JSON(code, nil)
	}
}
